<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Users extends CI_Controller {

 function __construct() {
   parent::__construct();
   $this->load->model('users_model','',TRUE);
   $this->load->model('movements_model');
 }

 //Loads the users view
 function index() {
   if($this->session->userdata('logged_in')) {
     $session_data = $this->session->userdata('logged_in');
     $data['ds_usuario'] = $session_data['ds_usuario'];
     $this->load->view('users_view', $data);
   } else {
     redirect('login', 'refresh');
   }
 }

//Validates the provided information to login the user
function dologin() {
   if($this->input->post("loginuser") && $this->input->post("loginpassword") && $this->input->post("logincountry")){
    $username = $this->input->post('loginuser');
    $password = $this->input->post('loginpassword');
    $country = $this->input->post('logincountry'); 
    //query the database
    $result = $this->users_model->login($username, $password, $country);
    if($result) {
      $sess_array = array();
      foreach($result as $row) {
        $sess_array = array(
          'id_usuario' => $row->id_usuario,
          'ds_usuario' => $row->ds_usuario,
          'id_tipousuario' => $row->id_tipousuario,
          'id_pais' => $row->id_pais
        );
      $this->session->set_userdata('logged_in', $sess_array);

      $session_data = $this->session->userdata('logged_in');
      $idUser = $session_data['id_usuario'];
      $idCountry = $session_data['id_pais'];
      //$date = date('Y-m-d h:i:s');
      $timezone = new DateTimeZone("UTC");
      $date = new DateTime("now", $timezone);
      $date = $date->format("Y-m-d\TH:i:s");
      $this->movements_model->savemovement('11', $idUser, $idCountry, $date) ;
      echo json_encode(array("response" => "success"));
      }
    } else {
        echo json_encode(array("response" => "failed"));
    }
   } else {
      echo json_encode(array("response" => "incomplete_form"));
   }
 }

function saveuser() {
  if($this->input->post("username") && $this->input->post("usernick") 
    && $this->input->post("usertype") && $this->input->post("usercountry") 
    && $this->input->post("userdorp") && $this->input->post("userstate")){
      $username = $this->input->post('username');
      $nickname = $this->input->post('usernick');
      $password = $this->input->post('userpass');
      $usertype = $this->input->post('usertype');
      $country = $this->input->post('usercountry'); 
      $userdorp = $this->input->post('userdorp');
      $userstate = $this->input->post('userstate');
      //query the database
      $result = $this->users_model->save($username, $nickname, $password, $usertype, $country, $userdorp, $userstate);
      if($result) {
        echo json_encode(array("response" => "success"));
      } else {
        echo json_encode(array("response" => "failed"));
      }
  } else {
      echo json_encode(array("response" => "incomplete_form"));
  }

 }

 function returntypeusers() {
   //query the database
   $result = $this->users_model->gettypeusers();
   if($result) {
     echo json_encode($result);
   }
 }

 function returnallusers() {
   //query the database
   $result = $this->users_model->getallusers();
   if($result) {
     echo json_encode($result);
   }
 }

 function returnuserbyid($id) {
   //query the database
   $result = $this->users_model->getuserbyid($id);
   if($result) {
     echo json_encode($result);
   }
 }
}

?>